package com.mzj.saas.rent;

import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.MaterialPhoto;
import com.haixiaoke.saas.repository.service.IMaterialPhotoService;
import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.mservice.oss.service.OSSService;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片素材Controller
 *
 * @author panda
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/material/photo")
public class MaterialPhotoController extends BaseController {
    @Autowired
    private IMaterialPhotoService materialPhotoService;
    @Autowired
    private LoginUserUtil loginUserUtil;
    @Autowired
    private OSSService ossService;

    /**
     * 查询图片素材列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MaterialPhoto materialPhoto) {
        startPage();
        List<MaterialPhoto> list = materialPhotoService.selectMaterialPhotoList(materialPhoto);
        return getDataTable(list);
    }

    /**
     * 导出图片素材列表
     */
    /*@PostMapping("/export")
    public void export(HttpServletResponse response, MaterialPhoto materialPhoto) {
        List<MaterialPhoto> list = materialPhotoService.selectMaterialPhotoList(materialPhoto);
        ExcelUtil<MaterialPhoto> util = new ExcelUtil<MaterialPhoto>(MaterialPhoto.class);
        util.exportExcel(response, list, "图片素材数据");
    }*/

    /**
     * 获取图片素材详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialPhotoService.selectMaterialPhotoById(id));
    }

    /**
     * 新增图片素材
     */
    @PostMapping
    public AjaxResult add(@RequestParam(value = "groupId") Long groupId, @RequestParam(value = "files") List<MultipartFile> files) {
        if (files == null || files.isEmpty()) {
            return error("图片获取异常");
        }
        if (groupId == null) {
            return error("请选择素材组");
        }
        List<String> photoUrlList = new ArrayList<>();
        for (MultipartFile file : files) {
            ResultBean<Object> resultBean = ossService.putFile(null, file, "material/photo");
            if (StatusCode.SUCCESS_CODE_10000.getErrorCode().equals(resultBean.getCode())) {
                photoUrlList.add((String) resultBean.getResultData());
            }
        }
        Long orgId = loginUserUtil.getOrgId();
        String userName = loginUserUtil.getName();
        return toAjax(materialPhotoService.insertMaterialPhoto(groupId, photoUrlList, orgId, userName));
    }

    /**
     * 修改图片素材属于哪个素材组
     */
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody MaterialPhoto materialPhoto) {
        if (materialPhoto.getPhotoIds() == null || materialPhoto.getPhotoIds().length == 0) {
            return error("请选择图片");
        }
        materialPhoto.setUpdateBy(loginUserUtil.getName());
        return toAjax(materialPhotoService.updateMaterialPhoto(materialPhoto));
    }

    /**
     * 删除图片素材
     */
    @DeleteMapping("/{photoIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] photoIds) {
        if (photoIds == null || photoIds.length == 0) {
            return error("请选择数据");
        }
        MaterialPhoto photo = new MaterialPhoto();
        photo.setUpdateBy(loginUserUtil.getName());
        photo.setUpdateTime(DateUtil.date());
        photo.setPhotoIds(photoIds);
        return toAjax(materialPhotoService.softDeleteMaterialPhotoByIds(photo));
    }
}
